package com.longbo.leaf.exception.handler;

import cn.hutool.core.collection.CollUtil;
import com.longbo.leaf.common.ApiResponse;
import com.longbo.leaf.common.BaseException;
import com.longbo.leaf.common.Status;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public ApiResponse handlerException(Exception e) {

      if (e instanceof BaseException) {
            log.error("【基础异常拦截】DataManagerException: 状态码 {}, 异常信息 {}", ((BaseException) e).getCode(), e.getMessage());
            return ApiResponse.ofException((BaseException) e);
        }
      if (e instanceof AccessDeniedException){
          log.error("【权限不足异常拦截】: 异常信息 {}",e.getMessage());
          return ApiResponse.ofStatus(Status.ACCESS_DENIED);
      }
        log.error("【全局异常拦截】: 异常信息 {} ", e.getMessage());
        return ApiResponse.ofMessage(e.getMessage());
    }

}
